#Cria uma instancia de um objeto de simulacao
set ns [new Simulator]

#Define a cor dos pacotes
$ns color 1 Pink
$ns color 2 Blue
$ns color 3 Red
# Estabelece o protocolo de roteamento de vetor distancia - CAMADA DE REDE
$ns rtproto DV

# Abre o arquivo q o NAM  ultilizara
set nf [open out.nam w]
$ns namtrace-all $nf

#Rotina em que o arquivo de simulacao sera criado e executado
proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
}

# Criação dos nós
for {set i 0} {$i < 20} {incr i} {
set n($i) [$ns node]
}


#CRIO O CANAL DE COMUNICAÇÃO - ENLACE
#atraso de cabo =  tempo da origem pro destino
$ns duplex-link $n(0) $n(1) 100Mb 2ms DropTail   
$ns duplex-link $n(1) $n(2) 100Mb 2ms DropTail   
$ns duplex-link $n(2) $n(3) 100Mb 2ms DropTail   
        
        
$ns duplex-link $n(0) $n(4) 1Mb 1.85ms DropTail   
$ns duplex-link $n(4) $n(8) 1Mb 1.85ms DropTail   
$ns duplex-link $n(8) $n(12) 1Mb 1.85ms DropTail   
$ns duplex-link $n(12) $n(16) 1Mb 1.85ms DropTail   
        
$ns duplex-link $n(1) $n(5) 1Mb 1.85ms DropTail   
$ns duplex-link $n(5) $n(9) 1Mb 1.85ms DropTail   
$ns duplex-link $n(9) $n(13) 1Mb 1.85ms DropTail   
$ns duplex-link $n(13) $n(17) 10Mb 1.85ms DropTail   
      
$ns duplex-link $n(2) $n(6) 1Mb 1.85ms DropTail   
$ns duplex-link $n(6) $n(10) 1Mb 1.85ms DropTail   
$ns duplex-link $n(10) $n(14) 1Mb 1.85ms DropTail   
$ns duplex-link $n(14) $n(18) 1Mb 1.85ms DropTail   
        
$ns duplex-link $n(3) $n(7) 1Mb 1.85ms DropTail   
$ns duplex-link $n(7) $n(11) 1Mb 1.85ms DropTail   
$ns duplex-link $n(11) $n(15) 1Mb 1.85ms DropTail   
$ns duplex-link $n(15) $n(19) 1Mb 1.85ms DropTail   

#Dou as posiçoes dos links
$ns duplex-link-op $n(0) $n(1) orient right
$ns duplex-link-op $n(1) $n(2) orient right
$ns duplex-link-op $n(2) $n(3) orient right

$ns duplex-link-op $n(0) $n(4) orient down
$ns duplex-link-op $n(4) $n(8) orient down
$ns duplex-link-op $n(8) $n(12) orient down
$ns duplex-link-op $n(12) $n(16) orient down

$ns duplex-link-op $n(1) $n(5) orient down
$ns duplex-link-op $n(5) $n(9) orient down
$ns duplex-link-op $n(9) $n(13) orient down
$ns duplex-link-op $n(13) $n(17) orient down

$ns duplex-link-op $n(2) $n(6) orient down
$ns duplex-link-op $n(6) $n(10) orient down
$ns duplex-link-op $n(10) $n(14) orient down
$ns duplex-link-op $n(14) $n(18) orient down

$ns duplex-link-op $n(3) $n(7) orient down
$ns duplex-link-op $n(7) $n(11) orient down
$ns duplex-link-op $n(11) $n(15) orient down
$ns duplex-link-op $n(15) $n(19) orient down


#Crio os custos do links - CAMADA DE REDE
$ns cost $n(0) $n(1) 1
$ns cost $n(1) $n(2) 1
$ns cost $n(2) $n(3) 1
$ns cost $n(0) $n(4) 1
$ns cost $n(4) $n(8) 1
$ns cost $n(8) $n(12) 1
$ns cost $n(12) $n(16) 1
$ns cost $n(1) $n(5) 1
$ns cost $n(5) $n(9) 1
$ns cost $n(9) $n(13) 1
$ns cost $n(13) $n(17) 1
$ns cost $n(2) $n(6) 1
$ns cost $n(6) $n(10) 1
$ns cost $n(10) $n(14) 1
$ns cost $n(14) $n(18) 1
$ns cost $n(3) $n(7) 1
$ns cost $n(7) $n(11) 1
$ns cost $n(11) $n(15) 1
$ns cost $n(15) $n(19) 1


        
################  CAMADA DE  TRANSPORTE ######################


#A geração de tráfego no NS-2 é baseada em 
#duas classes de objetos, a classe
#Agente e a classe Aplicação . 
#Cada nó na rede que necessite enviar ou receber dados
#deve ter um agente sobre ele. No topo de um agente roda uma aplicação. 
#A aplicação determina que tipo de tráfego é simulado
#Os agentes criam um canal de
#comunicação entre os nós transmissor e receptor. 




######################CAMADA DE TRANSPORTE#####################

#Seta a coneccao udp0
set udp0 [new Agent/UDP]
$ns 	attach-agent $n(0) $udp0
set null [new Agent/Null]
$ns attach-agent $n(16) $null
$ns connect $udp0 $null
$udp0 set fid_ 1

  

#Seta as caracteristicas do pacote
#atraso de transmissao...atraso dentro do rot
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp0
$cbr set type_ CBR
$cbr set packet_size_ 65535
$cbr set interval_ 0.1
$cbr set rate_ 1Mb
$cbr set random_ false

###############################################################
#Seta a coneccao udp1
set udp1 [new Agent/UDP]
$ns 	attach-agent $n(1) $udp1
set null [new Agent/Null]
$ns attach-agent $n(17) $null
$ns connect $udp1 $null
$udp1 set fid_ 1

  

# Create a VBR traffic source and attach it to node1
set vbr [new Application/Traffic/Exponential]
$vbr set packetSize_ 2800
$vbr set rate_ 600k
$vbr set burst_time_ 150ms
$vbr set idle_time_ 100ms
$vbr attach-agent $udp1 


####################################################################
#Create a TCP agent and attach it to node n1 --1 to 6
# setup TCP connections
#Setup a TCP connection
#esse aqui é o tahoe :D
set tcp [new Agent/TCP]

$tcp set class_ 2

$ns attach-agent $n(2) $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n(10) $sink


$ns connect $tcp $sink

$tcp set fid_ 1

#Setup a FTP over TCP connection
#o tcp sozinho nao manda o pacote, entao tenho que rodar uma aplicaçao ftp 
#para enviar os pacotes
#o tcp ele responde entao agente ve os pacotinhos pequenos voltando dizendo q pegou

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp set type_ FTP
################################################################################




####################################################################
#Create a TCP agent and attach it to node n1 --1 to 6
# setup TCP connections
#Setup a TCP connection
#esse aqui é o tahoe :D
set tcp1 [new Agent/TCP/Reno]

$tcp1 set class_ 2

$ns attach-agent $n(3) $tcp1

set sink1 [new Agent/TCPSink]

$ns attach-agent $n(11) $sink1


$ns connect $tcp1 $sink1

$tcp1 set fid_ 1

#Setup a FTP over TCP connection
#o tcp sozinho nao manda o pacote, entao tenho que rodar uma aplicaçao ftp 
#para enviar os pacotes
#o tcp ele responde entao agente ve os pacotinhos pequenos voltando dizendo q pegou

set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

$ftp1 set type_ FTP
################################################################################




#Agenda os eventos de inicio de envio de pacotes e fim
#Schedule events for the CBR agents
$ns at 2.0 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 1.0 "$ftp1 start"
$ns at 2.0 "$vbr start"


#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "$cbr stop"
$ns at 5.0 "$vbr stop"
$ns at 6.0 "$ftp stop"
$ns at 6.0 "$ftp1 stop"
#Detach tcp and sink agents (not really necessary)

$ns at 4.5 "$ns detach-agent $n(2) $tcp ; $ns detach-agent $n(10) $sink"




#$ns at 5.0 "$tcp stop"
$ns at 6 "finish"



#Run the simulation
$ns run 
